package com.bruce.codeFactory.enumDemo;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author luotuan
 * @Description
 * @create 2019-05-05 21:38
 **/
public class EnumUtil {
    private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();

    /**
     * 获取随机的枚举类型
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> T random(Class<T> clazz) {
        return random(clazz.getEnumConstants());
    }

    private static <T> T random(T[] values) {
        return values[RANDOM.nextInt(values.length)];
    }
}
